<?php

class Model_banner extends CI_Model{
	function __construct(){
        parent::__construct();
	}
	function addBanner(){
		$data = array(
		   'title' => $this->input->post("title",true) ,
		   'link' => $this->input->post("link_to",true),
		   'order_number' => 0
		);
		$this->db->insert('banner', $data);
		$insert_id=$this->db->insert_id();
		$hasilupload = $this->uploadImage($insert_id);
		if($hasilupload == "sukses"){ //kalo sukses upload
		}else{ //kalo error saat upload, delete lg
			$this->db->delete('banner', array('id' => $insert_id)); 
		}
		return $hasilupload;
	}
	
	function uploadImage($insert_id){
		if($_FILES['image']['name'] == "") return "You need to upload an image";
		
		$dir="banner";
		if(!is_dir ($dir)){
			$oldmask=umask(0);
			mkdir("banner",0777);
			umask($oldmask);
		}
		
		$path_parts = pathinfo($_FILES['image']['name']);
		$ext = $path_parts['extension'];
		
		$config['upload_path'] = $dir;
		$config['max_size']	= '2048'; // 2 MB
		$config['allowed_types'] = 'jpg|png|gif';
		$config['file_name'] = "temp_slide-".$insert_id.".".$ext;
		$config['overwrite'] = TRUE;
		//dimensi
		$config['max_width'] = "625";
		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload("image")){
			$error = $this->upload->display_errors();
			return $error;
		}
		else{
			$fileInfo = $this->upload->data();
			$fileName = $fileInfo['file_name'];
			
			$image_width = $fileInfo['image_width'];
			$image_height = $fileInfo['image_height'];
			$ratio = $image_width / $image_height;
			
			//cek min width
			if($image_width < 624){
				unlink($dir."/".$config['file_name']);
				return "Your image is too small";
			}
			if($ratio >= 2 && $ratio <= 2.1){
			}else{
				unlink($dir."/".$config['file_name']);
				return "Your image ratio is not between 2 and 2.1";
			}
			
			$newname = "slide-".$insert_id.".".$ext;
			rename($dir."/".$config['file_name'], $dir."/".$newname);
			//update the database to set the file name..
			$data = array(
				   'picture_link' => $newname
				);
			$this->db->where('id', $insert_id);
			$this->db->update('banner', $data);
			
			$hasil = "sukses";
			return $hasil;
		}
	}
	function getBannerList($params=array()){
		$this->db->select("*");
		$this->db->from("banner");
		
		if(isset($params['order_by']) ) $this->db->order_by($params['order_by']);
		if(isset($params['limit_1'])  && isset($params['limit_2']) ) $this->db->limit($params['limit_1'],$params['limit_2']);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	function delete($banner_id){
		$dir="banner";
		$this->db->select("*");
		$this->db->from("banner");
		$this->db->where("id",$banner_id);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->row_array();
			$filename = $hasil['picture_link'];
			unlink($dir."/".$filename);
		}
		$this->db->where('id', $banner_id);
		$this->db->delete('banner'); 
	}
	
	function change_banner_sequence($banner_id,$urutan){
		$this->db->query("update banner set `order_number`=`order_number`+1 where `order_number` > 0 and `order_number` >= ".$urutan);			
		$data = array(
		   'order_number' => $urutan
		);
		$this->db->where('id', $banner_id);
		$this->db->update('banner', $data);
		//rapikan ordernya..
		$this->finishing_order_sequence();
	}
	function finishing_order_sequence(){
		$this->db->select("*");
		$this->db->from("banner");
		$this->db->where('order_number >','0');
		$this->db->order_by('order_number asc');
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			$urutan = 1;
			foreach($hasil as $result){
				$urutanupdate = $urutan;
				if($urutan > 6) $urutanupdate = 0;
				$data = array(
				   'order_number' => $urutanupdate
				);
				$this->db->where('id', $result['id']);
				$this->db->update('banner', $data);
				$urutan++;
			}
		}
	}
}